//
//  TestTableViewCell.m
//  Advance_MVC
//
//  Created by Willian on 2021/5/2.
//

#import "TestTableViewCell.h"

@interface TestTableViewCell ()

@property (nonatomic, strong) UIButton *descBtn;
@property (nonatomic, strong) UIButton *addBtn;

@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *genderLab;

@property (nonatomic, strong) UILabel *ageLab;

@property (nonatomic, assign) int age;

@end

@implementation TestTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.genderLab];
    
    [self.contentView addSubview:self.descBtn];
    [self.contentView addSubview:self.ageLab];
    [self.contentView addSubview:self.addBtn];
    
    //布局
    self.nameLab.frame = CGRectMake(15, 30, 80, 40);
    self.genderLab.frame = CGRectMake(140, 30, 80, 40);
    
    self.descBtn.frame = CGRectMake(screenWidth - 3*(40 + 15), 30, 50, 40);
    self.ageLab.frame = CGRectMake(screenWidth - 2*(40 + 15), 30, 50, 40);
    self.addBtn.frame = CGRectMake(screenWidth - (40 + 15), 30, 50, 40);
    
    [self.descBtn addTarget:self action:@selector(descBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setAge:(int)age{
    _age = age;
    self.ageLab.text = [NSString stringWithFormat:@"%d", age];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeUserAge:withIndex:)]) {
        [self.delegate changeUserAge:age withIndex:self.indexPath.row];
    }
}

- (void)refreshUIWithUserModel:(UserModel *)userModel{
    self.nameLab.text = userModel.name;
    self.genderLab.text = userModel.gender;
    //cell里面会做显示处理
    self.age = userModel.age;
}

- (void)descBtnClick{
    //拿到前档的值 然后减一
    self.age = self.age - 1;
}

- (void)addBtnClick{
    self.age = self.age + 1;
}

- (UILabel *)nameLab{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = [UIColor blackColor];
    }
    return _nameLab;
}

- (UILabel *)genderLab{
    if (!_genderLab) {
        _genderLab = [[UILabel alloc] init];
        _genderLab.textColor = [UIColor blackColor];
    }
    return _genderLab;
}

- (UIButton *)descBtn{
    if (!_descBtn) {
        _descBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_descBtn setTitle:@"-" forState:UIControlStateNormal];
        [_descBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _descBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    }
    return _descBtn;
}

- (UILabel *)ageLab{
    if (!_ageLab) {
        _ageLab = [[UILabel alloc] init];
        _ageLab.textColor = [UIColor blackColor];
    }
    return _ageLab;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setTitle:@"+" forState:UIControlStateNormal];
        [_addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    }
    return _addBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
